//
//  SDURLCache.h
//  SDURLCache
// Copyright (c) 2010-2011 Olivier Poitrey <rs@dailymotion.com>
// Modernized to use GCD by Peter Steinberger <steipete@gmail.com>
//
//  Created by Olivier Poitrey on 15/03/10.
//  Copyright 2010 Dailymotion. All rights reserved.
//

#import <Foundation/Foundation.h>

/* Define SDURLCACHE_DEBUG=1 if you want to get some more info about the cached requests. */

@interface SDURLCache : NSURLCache
{
    @private
        BOOL _diskCacheInfoDirty;
        BOOL _ignoreMemoryOnlyStoragePolicy;
        BOOL _timerPaused;
        BOOL _shouldRespectCacheControlHeaders;
        NSUInteger _diskCacheUsage;
        NSTimeInterval _minCacheInterval;
        dispatch_source_t _maintenanceTimer;
}

/*
 * Defines the minimum number of seconds between now and the expiration time of a cacheable response
 * in order for the response to be cached on disk. This prevent from spending time and storage capacity
 * for an entry which will certainly expire before behing read back from disk cache (memory cache is
 * best suited for short term cache). The default value is set to 5 minutes (300 seconds).
 */
@property (nonatomic, assign) NSTimeInterval minCacheInterval;

/*
 * Allow the responses that have a storage policy of NSURLCacheStorageAllowedInMemoryOnly to be cached
 * on the disk anyway.
 *
 * This is mainly a workaround against cache policies generated by the UIWebViews: starting from iOS 4.2,
 * it always has a cache policy of NSURLCacheStorageAllowedInMemoryOnly.
 * The default value is YES
 */
@property (nonatomic, assign) BOOL ignoreMemoryOnlyStoragePolicy;

/*
 * Allow caching responses for a request with a cache policy that ignores the local cache.
 * Usually, these responses don't need to be cached, because a request that ignores the cache
 * once is likely to do so every time it is used.
 * 
 * The default value is NO.
 */
@property (nonatomic, assign) BOOL allowCachingResponsesToNonCachedRequests;

/*
 * If the server sends cache-control / pragma: no-cache headers, then the requests will not be cached.
 *
 * The default value is YES.
 */
@property (nonatomic, assign) BOOL shouldRespectCacheControlHeaders;

/*
 * Returns a default cache director path to be used at cache initialization. The generated path directory
 * will be located in the application's cache directory and thus won't be synced by iTunes.
 */
+ (NSString *)defaultCachePath;

/*
 * Checks if the provided URL exists in cache.
 */
- (BOOL)isCached:(NSURL *)url;

/*
 * Clears the receiver’s in-memory cache, removing all stored cached URL responses there.
 * Has no effect on the on-disk cache.
 */
- (void)removeAllCachedResponsesInMemory;

@end

#pragma mark - For subclasses only

@interface SDURLCache (PrivateMethods)

/*
 * This is the perfect place to strip some query parameters in case you are sending
 * OAuth information via query string instead of the Authorization request header.
 */
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;

@end
